package com.etone.smartAudit.onlyoffice.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etone.smartAudit.constant.WorkOrderConstant;
import com.etone.smartAudit.domain.work.WorkOrder;
import com.etone.smartAudit.domain.work.WorkRectifyTemplate;
import com.etone.smartAudit.service.WorkOrderService;
import com.etone.smartFlow.common.StringUtils;

/**
 * @author 张任豪
 */
public class WorkRectifyListener implements ReadListener<WorkRectifyTemplate> {

    private WorkOrderService workOrderService;

    public WorkRectifyListener(WorkOrderService workOrderService) {
        this.workOrderService = workOrderService;
    }

    @Override
    public void invoke(WorkRectifyTemplate workRectifyTemplate, AnalysisContext analysisContext) {
        String rectifyCode = workRectifyTemplate.getRectifyCode();
        //校验格式是否正确
        Integer rectifyResult = workRectifyTemplate.getRectifyResult();
        //校验格式是否正确
        Integer isStartCharge = workRectifyTemplate.getIsStartCharge();
        //校验是否以确定工单
        if (StringUtils.isNotEmpty(rectifyCode)){
            WorkOrder workOrder = workOrderService.getOne(new QueryWrapper<WorkOrder>().eq("vcRectifyCode", rectifyCode));
            workRectifyTemplate.setWorkId(workOrder.getIntId());
            if (!(rectifyResult.equals(0) || rectifyResult.equals(1) || rectifyResult.equals(2))){
                workRectifyTemplate.setIsError(true);
                workRectifyTemplate.setMassage("填写的整改结果格式有误！");
            } else if (!(isStartCharge.equals(0) || isStartCharge.equals(1))){
                workRectifyTemplate.setIsError(true);
                workRectifyTemplate.setMassage("是否启动问责，请填写0或1！");
            }else {
                workRectifyTemplate.setIsError(false);
                workRectifyTemplate.setMassage("success");
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
